package com.CMS.client.ui.view;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

import com.CMS.client.CMSConstants;
import com.CMS.client.PageService;
import com.CMS.client.PageServiceAsync;
import com.CMS.client.TemplateService;
import com.CMS.client.TemplateServiceAsync;
import com.CMS.server.domain.Page;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.smartgwt.client.types.Alignment;
import com.smartgwt.client.types.MultipleAppearance;
import com.smartgwt.client.types.TitleOrientation;
import com.smartgwt.client.util.SC;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.form.fields.events.ClickEvent;  
import com.smartgwt.client.widgets.form.fields.events.ClickHandler;
import com.smartgwt.client.widgets.form.DynamicForm;
import com.smartgwt.client.widgets.form.fields.ButtonItem;
import com.smartgwt.client.widgets.form.fields.RadioGroupItem;
import com.smartgwt.client.widgets.form.fields.SelectItem;
import com.smartgwt.client.widgets.layout.VLayout;

public class PageWritterView extends VLayout{

	private static final String DESCRIPTION = "PageWritterView";
	protected CMSConstants constants = GWT.create(CMSConstants.class);
	private SelectItem paginas;
	private LinkedHashMap<String, String> valueMap = new LinkedHashMap<String, String>();
	private DynamicForm form;
	private RadioGroupItem radioGroupItem;
	
	public PageWritterView(){
		super();
		
		paginas = new SelectItem();
		paginas.setTitle("P&aacute;ginas");
		paginas.setMultiple(true);
		paginas.setMultipleAppearance(MultipleAppearance.PICKLIST);
		
		PageServiceAsync rpc = GWT.create(PageService.class);
		rpc.fetch(new AsyncCallback<List<Page>>() {
			@Override
			public void onFailure(Throwable caught) {
				SC.say("Error: "+ caught.getMessage());
			}
			@Override
			public void onSuccess(List<Page> result) {
				for(int i=0; i<result.size(); i++){
					valueMap.put(Long.toString((result.get(i).getId())), result.get(i).getDescription() + 
												" <b>(" +result.get(i).getLang().getDescription() +")</b>");
				}
				paginas.setValueMap(valueMap);
				
				radioGroupItem = new RadioGroupItem();  
		        radioGroupItem.setName("Template");  
		        radioGroupItem.setColSpan("*");  
		        radioGroupItem.setRequired(true);  
		        radioGroupItem.setVertical(true);
		        radioGroupItem.setValueMap("Template Horizontal","Template Vertical");
		        radioGroupItem.setDefaultValue("Template Horizontal");
		        
		        ButtonItem btnGenerate = new ButtonItem(constants.generate());
		        btnGenerate.addClickHandler(new ClickHandler() {
					@Override
					public void onClick(ClickEvent event) {
						TemplateServiceAsync rpcTemplate = GWT.create(TemplateService.class);
						String[] selectedPages = paginas.getValues();
						int template = 0;
						if(((String)radioGroupItem.getValue()).equals("Template Horizontal"))
							template = 1;
						else
							template = 2;
						List<Long> pages = new ArrayList<Long>();
						for(int i=0; i<selectedPages.length; i++){
							pages.add(new Long(selectedPages[i]));
						}
						rpcTemplate.generatePageFile(pages, 
													 template, 
													 new AsyncCallback<Void>() {
														@Override
														public void onFailure(
																Throwable caught) {
															SC.say("Error: "+caught.getMessage());																
														}

														@Override
														public void onSuccess(
																Void result) {
															
														}
													});
					}
				});
				
				form = new DynamicForm();
				form.setWidth(400);
				form.setGroupTitle("Generador de P&aacute;ginas");
				form.setIsGroup(true);
		        form.setTitleOrientation(TitleOrientation.LEFT);   
		        form.setAlign(Alignment.CENTER);
		        form.setPadding(20);
		        form.setFields(paginas,radioGroupItem,btnGenerate);
		        
		        drawView();
			}
		});		
		
        
        
		
		/*Button btnGenerate = new Button(constants.generate());
		btnGenerate.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				PageServiceAsync page = GWT.create(PageService.class);
				final TemplateServiceAsync rpc = GWT.create(TemplateService.class);
				page.getVisiblePages(new AsyncCallback<List<Page>>() {
					
					@Override
					public void onSuccess(List<Page> result) {
						for(int i=0; i<result.size(); i++){
							PageModel model = new PageModel();
							if(result.get(i).getLastVersion() != null){
								model.setContent(result.get(i).getLastVersion().getContent());
							}else model.setContent("");
							rpc.generatePageFile(result.get(i).getName(), 
											   model.getHead()+model.getBody()+model.getContent()+model.getFinalBody()+model.getFooter(), 
											   new AsyncCallback<Void>() {								
								@Override
								public void onSuccess(Void result) {
									SC.say("Archivos generados exitosamente");								
								}								
								@Override
								public void onFailure(Throwable caught) {
									SC.say("Error al generar archivos");									
								}
							});							
						}
					}					
					@Override
					public void onFailure(Throwable caught) {
						SC.say("Error al generar archivos");						
					}
				});
			}			
		});	*/
	}
	
	private void drawView(){
		this.addMember(form);
		this.draw();
	}
	
	public static class Factory implements ContextAreaFactory
	 {
		 private String id;
		 
		 public Canvas create()
		 {
			 //hay que crear la vista que vamos a llamar
			 PageWritterView view = new PageWritterView();
			 id = view.getID();
			 return view;
	     }
		 
		 public String getID()
		 {
		   return id;
		 }

		 public String getDescription() 
		 {
		   return DESCRIPTION;
		 }
	 }
}
